package com.whynot.frame.transaction.service;

import com.whynot.frame.basic.mybatis.service.BaseServiceImpl;
import com.whynot.frame.transaction.entities.SysUser;
import com.whynot.frame.transaction.mapper.TrasactionMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
public class TransactionService extends BaseServiceImpl<TrasactionMapper,SysUser>{
    //这个不会回滚，这个会看注解事务和配置事务的order执行顺序，最好是两个事务配置不要有冲突
    @Transactional(noRollbackFor = RuntimeException.class)
    public void insetTest(){
        insert(new SysUser("tongbeiinsetTest","痛恶必"));
        deleteById("1000");
        throw  new RuntimeException();
    }
    //会回滚
    public void insertTest2(){
        insert(new SysUser("tongbeiinsetTest2","痛恶必"));
        deleteById("1000");
        throw  new RuntimeException();
    }
    //不会回，因为事务切面没有扫描到，同时又没有加上对应的事务注解
    public void test3Insert(){
        insert(new SysUser("tongbeitest3Insert","痛恶必"));
        deleteById("1000");
        throw  new RuntimeException();
    }
    //会回滚
    @Transactional
    public void test4Insert(){
        insert(new SysUser("tongbeitest4Insert","痛恶必"));
        deleteById("1000");
        throw  new RuntimeException();
    }
    /**
     * 都会回滚，不管异常是在哪个方法上
     */
    public void insertTest(){
        insertT1();
        insertT2();
        //deleteT1();
        //deleteT2();
        //int i = 1/0;
        //int i = 1/0;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void mm(){
        //按照道理jj会插入进去，因为是本类直接调用的，事务会失效，不能作用在jj方法上。
        this.jj();
        // 通过获取本类的代理对象去嗲用jj，(TransactionService)AopContext.currentProxy().jj();
        this.baseMapper.insert(new SysUser("insertT1","痛恶必"));
        //insert(new SysUser("insertT1","痛恶必"));
    }

    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public void jj(){
        this.baseMapper.insert(new SysUser("insertT1","痛恶必"));
        int i = 1/0;
    }

    public void insertT1(){
        insert(new SysUser("insertT1","痛恶必"));
    }

    public void deleteT1(){
        deleteById("588ba3ce6d6a45c6a6895aedb828e017");
    }

    public void insertT2(){
        insert(new SysUser("insertT1","痛恶必"));
        int i = 1/0;
    }

    public void deleteT2(){
        deleteById("588ba3ce6d6a45c6a6895aedb828e017");
        int i = 1/0;
    }
}
